package org.example.infrastructure.adapter.repository;


import org.example.domain.shop.adapter.repository.IShopRepository;
import org.example.domain.shop.model.entity.ProductEntity;
import org.example.infrastructure.dao.IProductDao;
import org.example.infrastructure.dao.IProductInventoryDao;
import org.example.infrastructure.dao.IUserExtInfoDao;
import org.example.infrastructure.dao.po.ProductInventoryPO;
import org.example.infrastructure.dao.po.ProductPO;
import org.example.infrastructure.redis.IRedisService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.example.types.common.RedisConstants.*;

/**
 * @author yinghuashen
 * @version 1.0
 * @description
 * @date 2025/3/6 22:25
 */

@Repository
public class ShopRepository implements IShopRepository {

    @Resource
    private IRedisService redisService;

    @Resource
    private IProductDao productDao;

    @Resource
    private IProductInventoryDao productInventoryDao;

    @Resource
    private IUserExtInfoDao userExtInfoDao;

    @Override
    public ProductEntity getProductById(String productId) {
        // 逻辑过期
        String info_key = PRODUCT_INFO_PREFIX + productId;
        ProductPO productPO = redisService.getCacheObjectWithLogicalExpire(info_key, productId,
                ProductPO.class, productDao::getProductById, PRODUCT_INFO_TTL, TimeUnit.MINUTES);

        if (productPO == null) {
            return null;
        }

        // TODO 库存量的缓存需要和秒杀那边统一
        String stock_key = PRODUCT_STOCK_PREFIX + productId;
        ProductInventoryPO productInventoryPO = redisService.getCacheObjectWithLogicalExpire(stock_key, productId,
                ProductInventoryPO.class,
                productInventoryDao::getProductInventoryById, PRODUCT_STOCK_TTL, TimeUnit.MINUTES);
        if (productInventoryPO == null) {
            return null;
        }

        return ProductEntity.builder()
                .productId(productPO.getProductId())
                .productName(productPO.getProductName())
                .productDescription(productPO.getProductDescription())
                .originalPrice(productPO.getOriginalPrice())
                .discountPrice(productPO.getOriginalPrice())
                .totalStock(productInventoryPO.getTotalStock())
                .availableCount(productInventoryPO.getAvailableCount())
                .soldCount(productInventoryPO.getSoldCount())
                .build();
    }

    @Override
    public List<ProductEntity> getProductListByKeyword(String keyword, Integer pageNum, Integer pageSize) {
        //TODO: 后续实现快速查询
        int startIndex = (pageNum - 1) * pageSize;
        Map<String, Object> params = new HashMap<>();
        params.put("keyword", keyword);
        params.put("startIndex", startIndex);
        params.put("pageSize", pageSize);

        List<ProductPO> productListByKeyword = productDao.getProductListByKeyword(params);
        if(productListByKeyword == null || productListByKeyword.isEmpty()){
            return null;
        }

        List<ProductEntity> productEntityList = new LinkedList<>();
        for(ProductPO productPO : productListByKeyword){
            ProductEntity productEntity = getProductById(productPO.getProductId());
            productEntityList.add(productEntity);
        }
        return productEntityList;
    }

    @Override
    public int getUserLevel(String userId) {
        if(userId.contains("test")){
            return 1;
        }
        return userExtInfoDao.getUserLevel(userId);
    }
}
